//
//  MessageLikeTableViewCell.m
//  ExcellentHome
//
//  Created by 揭婧雯 on 16/10/15.
//  Copyright © 2016年 陈区. All rights reserved.
//

#import "MessageLikeTableViewCell.h"
#import "SEGMessageUserModel.h"

@implementation MessageLikeTableViewCell
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString*)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];

    self.backgroundColor = kColor_W;
    [self setUpSubviews];

    return self;
}

- (void)setUpSubviews
{
    // 图片
    self.iconView = [[UIImageView alloc] init];
    
    
    self.iconView.layer.masksToBounds = YES;
    [self addSubview:self.iconView];
    [self.iconView mas_makeConstraints:^(MASConstraintMaker* make) {
        make.left.equalTo(self.mas_left).with.offset(15);
        make.top.equalTo(self.mas_top).with.offset(15);
        make.width.mas_equalTo(38);
        make.height.mas_equalTo(38);
    }];
    [self.iconView seg_configWithRadius:2.f];
 
    // 性别标识
    self.sexImage = [[UIImageView alloc] init];
    self.sexImage.hidden = YES;
    [self addSubview:self.sexImage];
    [self.sexImage mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(self.iconView.mas_bottom).offset(2);
        make.right.equalTo(self.iconView.mas_right).offset(2);
    }];

    // 名称
    self.nameLabel = [[UILabel alloc] init];
    self.nameLabel.textColor = kColor_1;
    self.nameLabel.font = [UIFont systemFontOfSize:15];
    [self addSubview:self.nameLabel];
    [self.nameLabel mas_makeConstraints:^(MASConstraintMaker* make) {
        make.left.equalTo(self.iconView.mas_right).with.offset(10);
//        make.right.equalTo(self.mas_right).with.offset(-45);
        make.top.equalTo(self.mas_top).with.offset(18);
    }];

//    //来自
//    self.addressLabel = [[UILabel alloc] init];
//    self.addressLabel.textColor = kColor_3;
//    self.addressLabel.font = [UIFont systemFontOfSize:13];
//    [self addSubview:self.addressLabel];
//    [self.addressLabel mas_makeConstraints:^(MASConstraintMaker* make) {
//        make.left.equalTo(self.iconView.mas_right).with.offset(10);
//        make.right.equalTo(self.mas_right).with.offset(-10);
//        make.top.equalTo(self.nameLabel.mas_bottom).with.offset(10);
//        make.height.mas_equalTo(13);
//    }];

    //内容
    self.contentLabel = [[UILabel alloc] init];
    self.contentLabel.textColor = kColor_1;
    self.contentLabel.font = [UIFont systemFontOfSize:16];
    self.contentLabel.numberOfLines = 2;
    [self addSubview:self.contentLabel];
    [self.contentLabel mas_makeConstraints:^(MASConstraintMaker* make) {
        make.left.equalTo(self.iconView.mas_right).with.offset(10);
        make.right.equalTo(self.mas_right).with.offset(-10);
        make.top.equalTo(self.nameLabel.mas_bottom).with.offset(10);
        make.height.mas_equalTo(16);
    }];

    // 引用原文
    self.subMsgView = [[UIButton alloc] init];
    self.subMsgView.backgroundColor = kColor_bg;
    [self.subMsgView addTarget:self action:@selector(tapSubMessage) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:self.subMsgView];
    [self.subMsgView mas_makeConstraints:^(MASConstraintMaker* make) {
        make.left.equalTo(self.iconView.mas_right).with.offset(10);
        make.right.equalTo(self.mas_right).with.offset(-15);
        make.top.equalTo(self.contentLabel.mas_bottom).with.offset(10);
//        make.height.mas_equalTo(33);
    }];
    self.subMsgView.hidden = YES;

    self.subMsgLabel = [[UILabel alloc] init];
    self.subMsgLabel.textColor = kColor_1;
    self.subMsgLabel.font = [UIFont systemFontOfSize:13];
    self.subMsgLabel.numberOfLines = 2;
    [self.subMsgView addSubview:self.subMsgLabel];
    [self.subMsgLabel mas_makeConstraints:^(MASConstraintMaker* make) {
        make.left.equalTo(self.subMsgView.mas_left).with.offset(15);
        make.right.equalTo(self.subMsgView.mas_right).with.offset(-15);
        make.top.equalTo(self.subMsgView);
        make.bottom.equalTo(self.subMsgView);
    }];
    
    //时间
    self.timeLabel = [[UILabel alloc] init];
    self.timeLabel.textColor = kColor_3;
    self.timeLabel.font = [UIFont systemFontOfSize:11];
    [self addSubview:self.timeLabel];
    [self.timeLabel mas_makeConstraints:^(MASConstraintMaker* make) {
        make.top.equalTo(self.subMsgView.mas_bottom).with.offset(10);
        make.bottom.equalTo(self.mas_bottom).with.offset(-15);
        make.left.equalTo(self.iconView.mas_right).with.offset(10);
        make.height.mas_equalTo(11);
        make.width.mas_greaterThanOrEqualTo(0);
    }];

}

- (void)showWithModel:(MessageModel*)msgModel
{
    self.model = msgModel;
    NSString* iconString = [NSString stringWithFormat:@"%@/%@", kClientImageRootURL, msgModel.usericon];
    [self.iconView setImageWithURL:[NSURL URLWithString:iconString] placeholderImage:[UIImage imageWithContentsOfFile:SEGPicName(@"headimg82")]];
    
    self.nameLabel.text = msgModel.sender;

//    self.addressLabel.text = msgModel.lable;

    if (msgModel.createTime && msgModel.createTime.length >0) {
        NSString* currentDate = [SEGUtils stringDateByFormatString:kTimeFormatStringMin withDate:[NSDate date]];
        if ([[currentDate substringToIndex:10] isEqualToString:[msgModel.createTime substringToIndex:10]]) {
            self.timeLabel.text = [msgModel.createTime substringFromIndex:11];
        }
        else {
            self.timeLabel.text = [msgModel.createTime substringFromIndex:5];
        }
    }
    
    if ([msgModel.groupid hasPrefix:kMessageGroupLikePre]) {
        self.contentLabel.text = msgModel.message;
    }
    else if ([msgModel.groupid hasPrefix:kMessageGroupCommentPre]) {
        self.contentLabel.text = [NSString stringWithFormat:@"回复我：%@", msgModel.message];
    }

    //原文
    if (msgModel.submsg && msgModel.submsg.length > 0) {
        self.subMsgView.hidden = NO;

        NSDictionary* subMsgDic = [NSJSONSerialization JSONObjectWithData:[msgModel.submsg dataUsingEncoding:NSUTF8StringEncoding] options:NSJSONReadingAllowFragments error:nil];

        self.subMsgLabel.text = [subMsgDic objectForKey:@"title"];

        NSString* picUrl = [subMsgDic objectForKey:@"url"];
        if (picUrl && picUrl.length > 0) { //原文有图片
            NSArray* urlArr = [picUrl componentsSeparatedByString:@","];
            
            [self.subMsgView addSubview:self.subPicView];
            [_subPicView mas_makeConstraints:^(MASConstraintMaker* make) {
                make.left.equalTo(self.subMsgView.mas_left);
                make.top.equalTo(self.subMsgView).priorityHigh();
                make.bottom.equalTo(self.subMsgView).priorityHigh();
                make.width.mas_equalTo(54);
                make.height.mas_equalTo(54);
            }];
            
            [self.subMsgLabel mas_remakeConstraints:^(MASConstraintMaker* make) {
                make.left.equalTo(self.subPicView.mas_right).with.offset(10);
                make.right.equalTo(self.subMsgView.mas_right).with.offset(-15);
                make.top.equalTo(self.subMsgView);
                make.bottom.equalTo(self.subMsgView);
            }];

            [self.subPicView setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@%@", kClientImageRootURL, urlArr[0]]]
                            placeholderImage:[UIImage imageWithContentsOfFile:SEGPicName(@"pic_default_300_300")] options:SDWebImageRetryFailed];
        }
        else {
            if (self.subPicView.superview) {
                [self.subPicView removeFromSuperview];
            }
            [self.subMsgLabel mas_remakeConstraints:^(MASConstraintMaker* make) {
                make.left.equalTo(self.subMsgView.mas_left).with.offset(15);
                make.right.equalTo(self.subMsgView.mas_right).with.offset(-15);
                make.top.equalTo(self.subMsgView);
                make.bottom.equalTo(self.subMsgView);
            }];
        }
    }
    else {
        self.subMsgView.hidden = YES;
    }
}

- (void)showUserInfoWithModel:(SEGMessageUserModel *)userModel {
    if (!userModel) return;
    
    [self.iconView setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@%@", kClientImageRootURL, userModel.icon]] placeholderImage:[UIImage imageWithContentsOfFile:SEGPicName(@"headimg40")]];
    self.nameLabel.text = userModel.nickName;
    
    // 设置用户性别
    if ([userModel.sex isEqualToString:@"1"]) {//女
        self.sexImage.image = [UIImage imageWithContentsOfFile:SEGPicName(@"icon_woman")];
    } else if ([userModel.sex isEqualToString:@"2"]) {
        self.sexImage.image = [UIImage imageWithContentsOfFile:SEGPicName(@"icon_man")];
    } else {
        self.sexImage.image = nil;
    }
}

- (void)tapSubMessage {
    [self.delegate didTapSubMessageWithModel:self.model];
}

- (void)headButtonDidTap:(UIButton *)sender {
    NSDictionary *params = @{@"requestUserId":self.model.sendUserId ?: @""};
    UIViewController *vc = [[SEGUhomeMediator sharedInstance] seg_initSEGPersonalHomePageNewViewController:params];
    vc.hidesBottomBarWhenPushed = YES;
    [self.viewController.navigationController pushViewController:vc animated:YES];
}
- (UIImageView *)subPicView {
    if (!_subPicView) {
        _subPicView = [[UIImageView alloc] init];
        _subPicView.backgroundColor = kColor_1;
    }
    return _subPicView;
}

@end
